package com.android.danaobank.mvp.presenter;

import com.android.danaobank.api.ApiClient;
import com.android.danaobank.model.HttpResponse;
import com.android.danaobank.model.response.MallHeadline;
import com.android.danaobank.mvp.view.MallHeadlinesView;
import com.lg.meng.base.BasePresenter;

import java.util.List;

public class MallHeadlinesPresenter extends BasePresenter<MallHeadlinesView> {

    private List<MallHeadline> mallHeadlineList;
    private int position;

    public void loadMallHeadlines() {
        ApiClient.getInstance().mallHeadlines()
                .subscribe(simpleObserver(new Callback<HttpResponse<List<MallHeadline>>>() {
                    @Override
                    public void resultSuccess(HttpResponse<List<MallHeadline>> data) {
                        position = 0;
                        mallHeadlineList = data.getData();
                        loadNext();
                    }

                    @Override
                    public void resultError(Throwable e) {
                    }
                }));
    }

    public void loadNext() {
        view.loadMallHeadlinesSuccess(getNextMallHeadline());
        position++;
        if (position == mallHeadlineList.size()) {
            position = 0;
        }
    }

    private MallHeadline getNextMallHeadline() {
        return mallHeadlineList.get(position);
    }
}
